package org.jmat.domain.entities;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 * This class represents a team.<br />
 * A person has one or several teams. <br />
 * A team has one or several persons.
 * 
 * @author Mathieu Chatelain
 * 
 */

@Entity
@Table(name = "TEAM")
public class Team implements Serializable {

	private static final long serialVersionUID = 6028307445045284337L;
	private Integer id;
	private String name;
	private Set<Person> persons = new HashSet<Person>();

	public Team(){
	}
	
	public Team(String name){
		this.name = name;
	}


	@Id
	@Column(name = "ID", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany(mappedBy="teams" , fetch=FetchType.LAZY)
	public Set<Person> getPersons() {
		return persons;
	}


	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
	
}
